package com.example.graduationdesignbackend.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.graduationdesignbackend.constants.ResponseCode;
import com.example.graduationdesignbackend.dto.PageDto;
import com.example.graduationdesignbackend.entity.Product;
import com.example.graduationdesignbackend.exception.ParkException;
import com.example.graduationdesignbackend.mapper.ProductMapper;
import com.example.graduationdesignbackend.query.ProductQuery;
import com.example.graduationdesignbackend.res.Res;
import com.example.graduationdesignbackend.service.IProductService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.graduationdesignbackend.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yuze
 * @since 2024-05-04
 */
@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements IProductService {

    @Autowired
    private IUserService userService;

    @Override
    public void createProduct(Product product) {
        Product product1 = product;
        product1.setId(RandomUtil.randomNumbers(9));
        product1.setStatus(0);
        product1.setUserId(StpUtil.getLoginIdAsString());
        product1.setCreateTime(new Date().getTime());
        product1.setUpdateTime(new Date().getTime());
        Boolean save = save(product1);
        if (!save) throw new ParkException(ResponseCode.SaveError);

    }

    @Override
    public Res editProduct(Product product) {
        Product product1 = BeanUtil.toBean(product,Product.class);
        product1.setStatus(0);
        product1.setUpdateTime(new Date().getTime());
        Boolean update = updateById(product1);
        if (!update) throw new ParkException(ResponseCode.UpdateError);
        return Res.yuze();
    }



    @Override
    public PageDto<Product> getMyProduct(ProductQuery productQuery) {
        String key = productQuery.getKey();
        int current = 1;
        if (!BeanUtil.isEmpty(productQuery.getCurrent())){
            current = productQuery.getCurrent();
        }
        int pageSize = 10;
        if (!BeanUtil.isEmpty(productQuery.getPageSize())){
            pageSize = productQuery.getPageSize();
        }
        Page<Product> page = Page.of(current,pageSize);
        page.addOrder(new OrderItem("create_time",false));
        Page<Product> p = new Page<>();
        if (BeanUtil.isEmpty(productQuery.getKey())){
            p = lambdaQuery()
                    .eq(Product::getUserId,StpUtil.getLoginIdAsString())
                    .page(page);
        }else {
            p = lambdaQuery()
                    .eq(Product::getUserId,StpUtil.getLoginIdAsString())
                    .like(Product::getTitle,key)
                    .page(page);
        }
        PageDto<Product> dto = new PageDto<>();
        dto.setTotal(p.getTotal());
        dto.setPages(p.getPages());
        dto.setPageSize(p.getSize());
        dto.setCurrent(p.getCurrent());
        dto.setList(p.getRecords());
        return dto;
    }

    @Override
    public Res listingProducts(String id) {
        Product product = getById(id);
        if (BeanUtil.isEmpty(product)) throw new ParkException(ResponseCode.Fail,"未查询到此商品");
        if (!product.getStatus().equals(0))throw new ParkException(ResponseCode.Fail);
        product.setStatus(1);
        Boolean update = updateById(product);
        if (!update) throw new ParkException(ResponseCode.UpdateError);
        return Res.yuze();
    }

    @Override
    public Res delistingProducts(String id) {
        Product product = getById(id);
        if (BeanUtil.isEmpty(product)) throw new ParkException(ResponseCode.Fail,"未查询到此商品");
        if (!product.getStatus().equals(9))throw new ParkException(ResponseCode.Fail);
        product.setStatus(0);
        Boolean update = updateById(product);
        if (!update) throw new ParkException(ResponseCode.UpdateError);
        return Res.yuze();
    }

    @Override
    public Res examine(String id) {
        Product product = getById(id);
        if (BeanUtil.isEmpty(product)) throw new ParkException(ResponseCode.Fail,"未查询到此商品");
        if (!product.getStatus().equals(1))throw new ParkException(ResponseCode.Fail);
        product.setStatus(9);
        Boolean update = updateById(product);
        if (!update) throw new ParkException(ResponseCode.UpdateError);
        return Res.yuze();
    }

    @Override
    public Res examineFail(String id) {
        Product product = getById(id);
        if (BeanUtil.isEmpty(product)) throw new ParkException(ResponseCode.Fail,"未查询到此商品");
        if (!product.getStatus().equals(1))throw new ParkException(ResponseCode.Fail);
        product.setStatus(5);
        Boolean update = updateById(product);
        if (!update) throw new ParkException(ResponseCode.UpdateError);
        return Res.yuze();
    }

    @Override
    public PageDto<Product> getProductPage(ProductQuery productQuery) {
        Integer status = productQuery.getStatus();
        String key = productQuery.getKey();
        String userId = productQuery.getUserId();
        int current = 1;
        if (!BeanUtil.isEmpty(productQuery.getCurrent())){
            current = productQuery.getCurrent();
        }
        int pageSize = 10;
        if (!BeanUtil.isEmpty(productQuery.getPageSize())){
            pageSize = productQuery.getPageSize();
        }
        Page<Product> page = Page.of(current,pageSize);
        page.addOrder(new OrderItem("create_time",false));
        Page<Product> p = new Page<>();
        if (BeanUtil.isEmpty(productQuery.getKey())){
            if (BeanUtil.isEmpty(productQuery.getUserId())){
                p = lambdaQuery()
                  .eq(Product::getStatus,status)
                  .page(page);
            }else{
                p = lambdaQuery()
                  .eq(Product::getUserId,userId)
                  .eq(Product::getStatus,status)
                  .page(page);
            }
            
        }else {
            if (BeanUtil.isEmpty(productQuery.getUserId())){
                p = lambdaQuery()
                  .like(Product::getTitle,key)
                  .eq(Product::getStatus,status)
                  .page(page);
            }else {
                p = lambdaQuery()
                  .like(Product::getTitle,key)
                  .eq(Product::getUserId,userId)
                  .eq(Product::getStatus,status)
                  .page(page);
            }
            
        }
        PageDto<Product> dto = new PageDto<>();
        dto.setTotal(p.getTotal());
        dto.setPages(p.getPages());
        dto.setPageSize(p.getSize());
        dto.setCurrent(p.getCurrent());
        dto.setList(p.getRecords());
        return dto;
    }

}
